package com.alfayun.fiscalriskanalysis.user;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Entity
@Table(name = "user")
public class User {

	@Column(name = "user_id", updatable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	private int id;
	public String username;
	public String password;
	public String phone;
	public String salt = UUID.randomUUID().toString();
	@Enumerated(EnumType.STRING)
	public Role role;
	public Integer invitationId;//邀请人id
	public String openId;
	public String avatar;

	public User() {
		// Empty.
	}

	public User(UserVo vo) {
		this.id = vo.id;
		this.username = vo.username;
		this.password = vo.password;
		this.role = vo.role;
		this.invitationId = vo.enterpriseId;
		this.openId = vo.openId;
	}

	public int getId() {
		return id;
	}

	public static User create(UserRepository repository, User user) {
		user.id = 0;
		user.password = new BCryptPasswordEncoder().encode(user.password);
		return repository.save(user);
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
